export const copyToClipboard = (text: string) =>
  navigator.clipboard.writeText(text);

/**
 * 解析url中哈希路由的参数
 * @param url 网址
 * @returns
 * @example
 * parseQuery("http://a.com/?a=1&b=2")=> {a: '1', b: '2'}
 */
export const parseQuery = (url: string) => {
  let q: IObj = {};
  url.replace(/([^?=&]+)=([^&]+)/g, (_, k: string, v: string) => (q[k] = v));
  return q;
};

interface IObj {
  [index: string]: any;
}

/**
 * 筛选对象属性
 * @param obj
 * @param props
 */
export const pick = (obj: IObj, ...props: string[]) =>
  Object.fromEntries(Object.entries(obj).filter(([k]) => props.includes(k)));

/**
 * 从dom元素中标签提取文本；
 * 去掉标签的文本
 * @param fragment 标签
 * @returns
 */
export const removeTag = (fragment: string) =>
  new DOMParser().parseFromString(fragment, "text/html").body.textContent || "";
